#pragma once

#include "RSA.h"
#include "BlowFish.h"
#include "smartalloc_wrap.h"
#include "Identity.h"

#include <vector>
#include <map>

namespace TTP {

   struct lt {
      bool operator()(const uint32_t a, const uint32_t b) {
         return a < b;
      }
   };
      
   class Group {
      friend class IIS;
      Group();
   protected:
      Address mAddress;
      std::map<uint32_t, Crypt::Identity*, lt> mParticipants;
      Crypt::RSA mGroupRSA;
      Crypt::BlowFish mGroupBlowfish;
      
      virtual ~Group() {
         FOR_EACH(it, mParticipants) {
            delete (it->second);
         }
      }
   public:
      Group(const Address& addr, std::vector<byte>& pubKey);
     
      // accessors
      Address getAddress() const;
      Crypt::Identity* getIdentity(uint32_t idHash);
      bool containsIdentity(uint32_t idHash);
      std::vector<Crypt::Identity*> getAllIdentities();
      uint32_t getLowestID();
      Crypt::RSA& getGroupRSA();
      Crypt::BlowFish getGroupBlowfish();
      
      // modifers
      void setAddress(const Address& addr);
      void addIdentity(Crypt::Identity*);

      // utility
      static Group * groupFactory() { return new Group(); }
      static Group * groupCloner(const Group& g) { return new Group(g); }

   };

}
